/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormBarangProduk.java
 *
 * Created on Feb 4, 2011, 2:35:45 AM
 */

package juwita;

import config.DBConnection;
import java.sql.*;
//import javax.swing.JTable;
import javax.swing.ButtonGroup;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumn;

/**
 *
 * @author Abah
 */
public class FormBarangProduk extends javax.swing.JInternalFrame {
    private DefaultTableModel DftTabMode;
    private boolean isGolLoaded = false;
    private boolean isJenisLoaded = false;    
    //private String urlValue="";
    java.sql.Connection conn = new DBConnection().connect();

  
    /** Creates new form FormBarangProduk */
    public FormBarangProduk() {
        initComponents();
        
        tbProduk.getTableHeader().setReorderingAllowed(false);

        //ButtonGroup group = new ButtonGroup();
        //group.add(rbKode);
       /// group.add(rbNamaProduk);
        
        loadDataGolongan();
        loadDataJenis();
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);        
        tampilData();

        
        
        
        //setDataProduk();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnTengah = new javax.swing.JPanel();
        spProduk = new javax.swing.JScrollPane();
        tbProduk = new javax.swing.JTable();
        pnAtas = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        txtNamaGol = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtKdProduk = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtKet = new javax.swing.JTextArea();
        btnSearch = new javax.swing.JButton();
        txtSearchProduk = new javax.swing.JTextField();
        btnNew = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtNamaJenis = new javax.swing.JTextField();
        cbGol = new javax.swing.JComboBox();
        cbSearchProduk = new javax.swing.JComboBox();
        btnShowAll = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        cbJenis = new javax.swing.JComboBox();
        txtNamaProduk = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setForeground(new java.awt.Color(0, 0, 0));
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Master Produk Barang");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/mnProduk.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        pnTengah.setBackground(new java.awt.Color(255, 255, 255));
        pnTengah.setLayout(new java.awt.BorderLayout());

        tbProduk.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbProduk.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tbProduk.setGridColor(new java.awt.Color(0, 0, 255));
        tbProduk.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbProdukMousePressed(evt);
            }
        });
        tbProduk.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbProdukKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbProdukKeyReleased(evt);
            }
        });
        spProduk.setViewportView(tbProduk);

        pnTengah.add(spProduk, java.awt.BorderLayout.CENTER);

        pnAtas.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas.setPreferredSize(new java.awt.Dimension(410, 280));
        pnAtas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        pnAtas.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 150, 70, 20));

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        pnAtas.add(btnEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 120, 70, -1));
        pnAtas.add(txtNamaGol, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 10, 230, -1));

        jLabel2.setText("Nama Produk");
        pnAtas.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, -1, -1));

        txtKdProduk.setEditable(false);
        txtKdProduk.setText("MCK.002.004");
        pnAtas.add(txtKdProduk, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 70, 90, -1));

        jLabel3.setText("Jenis");
        pnAtas.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        txtKet.setColumns(20);
        txtKet.setRows(5);
        jScrollPane1.setViewportView(txtKet);

        pnAtas.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 130, 240, 50));

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        pnAtas.add(btnSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 250, -1, -1));
        pnAtas.add(txtSearchProduk, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 230, 260, -1));

        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });
        pnAtas.add(btnNew, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 90, 70, -1));

        jLabel6.setText("Kode Produk");
        pnAtas.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));
        pnAtas.add(txtNamaJenis, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 40, 230, -1));

        cbGol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbGol.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbGolItemStateChanged(evt);
            }
        });
        pnAtas.add(cbGol, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 80, -1));

        cbSearchProduk.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kode Produk", "Nama Produk" }));
        pnAtas.add(cbSearchProduk, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 210, 110, -1));

        btnShowAll.setText("Show All");
        btnShowAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllActionPerformed(evt);
            }
        });
        pnAtas.add(btnShowAll, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 250, 80, -1));

        jLabel5.setText("Cari :");
        pnAtas.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, -1, -1));

        cbJenis.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MCK.002" }));
        cbJenis.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbJenisItemStateChanged(evt);
            }
        });
        cbJenis.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cbJenisFocusGained(evt);
            }
        });
        pnAtas.add(cbJenis, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 40, 80, -1));
        pnAtas.add(txtNamaProduk, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 100, 270, -1));

        jLabel7.setText("Golongan");
        pnAtas.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel8.setText("Keterangan");
        pnAtas.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, -1, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/mnProduk2.png"))); // NOI18N
        pnAtas.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 190, -1, -1));

        pnTengah.add(pnAtas, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(pnTengah, java.awt.BorderLayout.CENTER);

        setSize(new java.awt.Dimension(463, 511));
    }// </editor-fold>//GEN-END:initComponents

    private void tbProdukMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbProdukMousePressed
        getDataFromProduk();
}//GEN-LAST:event_tbProdukMousePressed

    private void tbProdukKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbProdukKeyPressed
        // TODO add your handling code here:
        getDataFromProduk();
}//GEN-LAST:event_tbProdukKeyPressed

    private void tbProdukKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbProdukKeyReleased
        // TODO add your handling code here:
        getDataFromProduk();
}//GEN-LAST:event_tbProdukKeyReleased

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if(btnDelete.getText().equals("Cancel")){
            kosongkan();
            tampilData();
        }else if(btnDelete.getText().equals("Delete")){
            deleteData();
        }
     
}//GEN-LAST:event_btnDeleteActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        //BARU KOPAS
        if(btnEdit.getText().equals("Save")){
            if(txtNamaProduk.getText().equals("") ) {
                JOptionPane.showMessageDialog(this, "Nama Produk harus diisi!",
                        "Informasi",JOptionPane.INFORMATION_MESSAGE);
            }else{
                insertData();

            }
        }else if (btnEdit.getText().equals("Edit")){
            editData();
        }

        //*
        //*/
}//GEN-LAST:event_btnEditActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        //BARU KOPAS

        tampilTabelSearch();
        txtKdProduk.setText("");
        txtNamaProduk.setText("");
        //txtKdProduk.requestFocus();
        //txtNamaGol.setText("");
        //txtNamaJenis.setText("");
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        //*
        //*/
}//GEN-LAST:event_btnSearchActionPerformed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        // TODO add your handling code here:
        txtNamaProduk.setText("");
        txtKet.setText("");
        txtSearchProduk.setText("");
        btnEdit.setText("Save");
        btnDelete.setText("Cancel");
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        
        setDataGolongan();
        setDataJenis();
        setKodeProduk();
}//GEN-LAST:event_btnNewActionPerformed

    private void cbGolItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbGolItemStateChanged
        // TODO add your handling code here:
        if(isGolLoaded==true){
            setDataGolongan();
            loadDataJenis();
            setDataJenis();
        }
}//GEN-LAST:event_cbGolItemStateChanged

    private void btnShowAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowAllActionPerformed
        // TODO add your handling code here:
        tampilData();
        txtSearchProduk.setText("");
}//GEN-LAST:event_btnShowAllActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        int clossing;
        clossing = JOptionPane.showConfirmDialog(this,
                "Tutup Laporan Barang?",
                "Konfirmasi",
                JOptionPane.YES_NO_OPTION);
        if (clossing == 0){
            this.dispose();
            FormMain.isfBrgProduk = false;
        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void cbJenisItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbJenisItemStateChanged
        // TODO add your handling code here:

        if(cbJenis.getItemCount()>0){
            setDataJenis();
            setKodeProduk();
        }else{
            System.out.println("data kosong woi");
        }
    }//GEN-LAST:event_cbJenisItemStateChanged

    private void cbJenisFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cbJenisFocusGained
        // TODO add your handling code here:        
    }//GEN-LAST:event_cbJenisFocusGained


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnShowAll;
    private javax.swing.JComboBox cbGol;
    private javax.swing.JComboBox cbJenis;
    private javax.swing.JComboBox cbSearchProduk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnAtas;
    private javax.swing.JPanel pnTengah;
    private javax.swing.JScrollPane spProduk;
    private javax.swing.JTable tbProduk;
    private javax.swing.JTextField txtKdProduk;
    private javax.swing.JTextArea txtKet;
    private javax.swing.JTextField txtNamaGol;
    private javax.swing.JTextField txtNamaJenis;
    private javax.swing.JTextField txtNamaProduk;
    private javax.swing.JTextField txtSearchProduk;
    // End of variables declaration//GEN-END:variables


    private void kosongkan(){
        txtNamaProduk.setText("");
        txtKet.setText("");
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        btnEdit.setText("Edit");
        btnDelete.setText("Delete");

    }
    private void tampilData(){
        Object[] Baris={"No","Kode","Nama"};
        DftTabMode=new DefaultTableModel(null,Baris);
        tbProduk.setModel(DftTabMode);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_produk");
            while (rslt.next()) {
                Integer no = rslt.getRow();
                String sKdProduk = rslt.getString("kdProduk");
                String sNamaProduk = rslt.getString("nmProduk");
                //String sKet = rslt.getString("ket");
                //String sTest = rslt.getString("test");

                String[] dataField = {Integer.toString(no), sKdProduk, sNamaProduk};//, sKet};
                //DftTabMode.addRow(dataField);

                DftTabMode.addRow(dataField);
                int widths[] = {50, 100, 250};
                for(int i=0;i<tbProduk.getColumnCount();i++){
                    TableColumn column = tbProduk.getColumnModel().getColumn(i);
                    column.setPreferredWidth(widths[i]);
                }                
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void tampilTabelSearch(){
        String srcby = "";
        if(cbSearchProduk.getSelectedItem().toString().equals("Kode Produk")){
            srcby = "kdProduk";
        }else{
            srcby = "nmProduk";
        }
        
        Object[] Baris={"No","Kode","Nama"};
        DftTabMode=new DefaultTableModel(null,Baris);
        tbProduk.setModel(DftTabMode);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbrg_produk WHERE UPPER(" + srcby +
                    ") like UPPER('%" +
                    txtSearchProduk.getText() + "%')");
            //int iNo = 0;
            while (rslt.next()) {
                //iNo = iNo + 1;
                //iNo.
                Integer x = rslt.getRow();
                //
                String sKdProduk = rslt.getString("kdProduk");
                String sNamaProduk = rslt.getString("nmProduk");
                //String sNamaKota = rslt.getString("ket");

                String[] dataField = {Integer.toString(x), sKdProduk, sNamaProduk};
                DftTabMode.addRow(dataField);
                int widths[] = {50, 100, 250};
                for(int i=0;i<tbProduk.getColumnCount();i++){
                    TableColumn column = tbProduk.getColumnModel().getColumn(i);
                    column.setPreferredWidth(widths[i]);
                }
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }
    private void deleteData(){
            try {
                int deleting;
                deleting = JOptionPane.showConfirmDialog(this,
                        "Apakah Anda yakin akan menghapus "

                        //+ cbKodeKota.getSelectedItem().toString() +" "
                        +txtKdProduk.getText() +"?",
                        "Konfirmasi",
                        JOptionPane.YES_NO_OPTION);
                if (deleting == 0){

                    PreparedStatement pStatement = null;
                    String sql = "DELETE FROM tbrg_produk " +
                            "WHERE kdProduk=? ";
                    pStatement = conn.prepareStatement(sql);

                    pStatement.setString(1, txtKdProduk.getText());

                    int intTambah = pStatement.executeUpdate();
                    if (intTambah >0){
                        JOptionPane.showMessageDialog(this,
                                "Delete sukses", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);

                    }else {
                        JOptionPane.showMessageDialog(this,
                                "Delete gagal", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);
                    }

                    pStatement.close();

                    kosongkan();
                    tampilData();
                }
            } catch(SQLException e){
                System.out.println("koneksi gagal " + e.toString());
            }
    }
    private void insertData(){

        java.sql.PreparedStatement pStatement = null;
        String sql = "INSERT INTO tbrg_produk " +
                "(kdProduk, kdJenis, nmProduk, ket, tgl_insert, tgl_update, user_id) " +
                "VALUES (?,?,?,?, now(),now(),?)";
        try {
            pStatement = conn.prepareStatement(sql);
            pStatement.setString(1, txtKdProduk.getText());
            String kdJenis = (String)cbJenis.getSelectedItem();
            pStatement.setString(2, kdJenis);
            pStatement.setString(3, txtNamaProduk.getText());
            pStatement.setString(4, txtKet.getText());
            pStatement.setString(5, "admin");

            int intTambah = pStatement.executeUpdate();
            if (intTambah>0){
                JOptionPane.showMessageDialog(this,
                        "Penambahan data produk sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this,
                        "Penambahan data produk gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            pStatement.close();
            //conn.close();

            kosongkan();
            tampilData();

        }
        catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }
    }

     private void editData(){        
        try {
            PreparedStatement pStatement = null;
            String sql = "UPDATE tbrg_produk " +
                    "SET nmProduk=?, ket=?, tgl_update=now() " +
                    "WHERE kdProduk=? ";
            pStatement = conn.prepareStatement(sql);

            pStatement.setString(1, txtNamaProduk.getText());
            pStatement.setString(2, txtKet.getText());
            pStatement.setString(3, txtKdProduk.getText());

            int intTambah = pStatement.executeUpdate();
            if (intTambah >0){
                JOptionPane.showMessageDialog(this,
                        "Edit sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);

            }else {
                JOptionPane.showMessageDialog(this,
                        "Edit gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }

            //tbBarangGol.get
            pStatement.close();
            kosongkan();
            tampilData();
        } catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }
    }

    private void getDataFromProduk(){
        Object oKdProduk, oNamaProduk, oKdGol, oKdJenis;//, oKet;//, oTest;

        oKdProduk = tbProduk.getValueAt(tbProduk.getSelectedRow(), 1);
        oNamaProduk = tbProduk.getValueAt(tbProduk.getSelectedRow(), 2);
        //oKet = tbBarangGol.getValueAt(tbBarangGol.getSelectedRow(), 2);
        //oTest = tbKota.getValueAt(tbKota.getSelectedRow(), 3);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_produk WHERE kdProduk=" +
                    "'" +oKdProduk+ "'");
            while (rslt.next()) {
                oKdGol = rslt.getString("kdJenis");
                cbGol.setSelectedItem(oKdGol.toString().substring(0, 3));
                oKdJenis = rslt.getString("kdJenis");
                //System.out.println("oi : " + oKdJenis.toString().substring(0,3));
                cbJenis.setSelectedItem(oKdJenis);                
                txtKet.setText(rslt.getString("ket"));
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        /*
        if(oTest == null){
            oTest = " ";
        }
         *
         */

        txtKdProduk.setText(oKdProduk.toString());
        txtNamaProduk.setText(oNamaProduk.toString());
        

        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        setDataGolongan();
    }

    private void loadDataGolongan(){
        cbGol.removeAllItems();

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol");
            while (rslt.next()) {
                Object data = (Object)rslt.getString("kdGol");
                cbGol.addItem(data);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        isGolLoaded = true;
        setDataGolongan();
    }
    private void loadDataJenis(){
        cbJenis.removeAllItems();
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_jenis " +
                    "WHERE kdGol='" + cbGol.getSelectedItem().toString() + "'");
            while (rslt.next()) {
                Object data = (Object)rslt.getString("kdJenis");
                cbJenis.addItem(data);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        if (cbJenis.getItemCount() > 0){
            isJenisLoaded = true;
        }else{
            isJenisLoaded = false;
        }
        //setDataJenis();
    }
    
    private void setDataGolongan(){
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol " +
                    "WHERE kdGol='" + cbGol.getSelectedItem().toString() + "'");
            while (rslt.next()) {
                txtNamaGol.setText(rslt.getString("nmGol"));
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void setDataJenis(){
        if(cbJenis.getItemCount() > 0 ){
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_jenis " +
                    "WHERE kdJenis='" + cbJenis.getSelectedItem().toString() + "'");
            while (rslt.next()) {
                txtNamaJenis.setText(rslt.getString("nmJenis"));
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        }else{
            System.out.println("Data jenis Kosong");
            txtNamaJenis.setText("");
        }
    }

    private void setDataProduk(){
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol " +
                    "WHERE kdGol='" + cbGol.getSelectedItem().toString() + "'");
            while (rslt.next()) {
                txtNamaGol.setText(rslt.getString("nmGol"));
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void setKodeProduk(){

        try {
            String kdProduk = cbJenis.getSelectedItem().toString();
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_produk " +
                    "WHERE kdJenis='" + kdProduk + "'");
            int x = 1;
            while (rslt.next()) {
                x = x + 1;//rslt.getRow();
                //System.out.println(x);
            }
            //System.out.println("kode jenis : " + kdProduk);

            java.text.DecimalFormat nft = new java.text.DecimalFormat("#000.###");
            nft.setDecimalSeparatorAlwaysShown(false);
            txtKdProduk.setText(kdProduk + "." + nft.format(x));
            //System.out.println(txtKdJenis.getText());
            //cbJenis.setSelectedItem(kdGol.toString());

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

    }
    
}
